/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MENU_SELECTION_MNDISH.java
 *
 * Created on Oct 8, 2010, 10:48:31 PM
 */

package cat200project;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author akmal
 */
public class MENU_SELECTION_MNDISH extends javax.swing.JFrame {

    Statement stat;
    Connection con;
    ResultSet rset;
    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String pass_id = "restaurant";
    private String pass_pw = "restaurant";
    private DefaultListModel M_Dish;
    String a;
    String b;
    String c;
    String newline = "\n";

    /** Creates new form MENU_SELECTION_MNDISH */
    public MENU_SELECTION_MNDISH() throws SQLException {
        super("MENU SELECTION");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
        populateData();
        takeout();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane4 = new javax.swing.JLayeredPane();
        jSeparator7 = new javax.swing.JSeparator();
        jSeparator8 = new javax.swing.JSeparator();
        Done = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        InfoPane = new javax.swing.JPanel();
        jLayeredPane5 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        MainList = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        SelectedArea = new javax.swing.JTextArea();
        Add = new javax.swing.JButton();
        Reset = new javax.swing.JButton();
        QuantityNum = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSeparator7.setBounds(0, 50, 700, 10);
        jLayeredPane4.add(jSeparator7, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator8.setBounds(0, 420, 700, 20);
        jLayeredPane4.add(jSeparator8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Done.setFont(new java.awt.Font("Times New Roman", 1, 12));
        Done.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        Done.setText("DONE");
        Done.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Done.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DoneActionPerformed(evt);
            }
        });
        Done.setBounds(580, 430, 100, 40);
        jLayeredPane4.add(Done, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("MENU PROFILE");
        jLabel1.setBounds(40, 10, 310, 50);
        jLayeredPane4.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        InfoPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        MainList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(MainList);

        jScrollPane1.setBounds(30, 40, 180, 240);
        jLayeredPane5.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        SelectedArea.setColumns(20);
        SelectedArea.setRows(5);
        jScrollPane2.setViewportView(SelectedArea);

        jScrollPane2.setBounds(390, 40, 170, 240);
        jLayeredPane5.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Add.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        Add.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        Add.setText("ADD >>");
        Add.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddActionPerformed(evt);
            }
        });
        Add.setBounds(250, 130, 100, 40);
        jLayeredPane5.add(Add, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Reset.setFont(new java.awt.Font("Times New Roman", 1, 12));
        Reset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        Reset.setText("RESET");
        Reset.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetActionPerformed(evt);
            }
        });
        Reset.setBounds(250, 200, 100, 40);
        jLayeredPane5.add(Reset, javax.swing.JLayeredPane.DEFAULT_LAYER);

        QuantityNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select...", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        QuantityNum.setBounds(260, 80, 80, 30);
        jLayeredPane5.add(QuantityNum, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Quantity :");
        jLabel4.setBounds(260, 50, 70, 20);
        jLayeredPane5.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Selected Food :");
        jLabel2.setBounds(390, 10, 90, 30);
        jLayeredPane5.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Available Main Dish :");
        jLabel3.setBounds(30, 10, 130, 30);
        jLayeredPane5.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jLabel8.setText("jLabel8");
        jLabel8.setBounds(-20, -30, 660, 380);
        jLayeredPane5.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout InfoPaneLayout = new javax.swing.GroupLayout(InfoPane);
        InfoPane.setLayout(InfoPaneLayout);
        InfoPaneLayout.setHorizontalGroup(
            InfoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
        );
        InfoPaneLayout.setVerticalGroup(
            InfoPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
        );

        InfoPane.setBounds(40, 80, 620, 310);
        jLayeredPane4.add(InfoPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel23.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract2.jpg"))); // NOI18N
        jLabel23.setText("jLabel23");
        jLabel23.setBounds(0, 0, 700, 500);
        jLayeredPane4.add(jLabel23, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 700, Short.MAX_VALUE)
            .addGap(0, 700, Short.MAX_VALUE)
            .addComponent(jLayeredPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
            .addGap(0, 500, Short.MAX_VALUE)
            .addComponent(jLayeredPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DoneActionPerformed
        try {
            new MENU_SELECTION_MAIN().setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(MENU_SELECTION_APP.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);
}//GEN-LAST:event_DoneActionPerformed

    private void AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddActionPerformed

        String food = (String) MainList.getSelectedValue();
        String get_query = "SELECT * FROM FOOD WHERE FOOD_NAME = '"+ food +"'";
        String store_query;
        String quantity = (String) QuantityNum.getSelectedItem();


        if(quantity.equals("Select...")) {
            JOptionPane.showMessageDialog(null, "Please select quantity number!");
        } else {
            try {
                rset = getResultSet(get_query);
                while(rset.next()) {
                    a = rset.getString("FOOD_NAME");
                    b = rset.getString("FOOD_PRICE");
                    c = rset.getString("FOOD_TYPE");
                }

                stat = con.createStatement();
                store_query = "INSERT INTO ORDER_LIST VALUES"+
                        "('"+ a +"','"+ b +"','"+ quantity +"','"+ c +"')";
                stat.executeUpdate(store_query);
                JOptionPane.showMessageDialog(null, "Main Dish added");
            } catch (SQLException ex) {
                Logger.getLogger(MENU_SELECTION_APP.class.getName()).log(Level.SEVERE, null, ex);
            }
            SelectedArea.append(a + newline);
        }

        // TODO add your handling code here:
}//GEN-LAST:event_AddActionPerformed

    private void ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetActionPerformed

        String message = "Are you sure to reset?";
        String title = "Reset Confirmation";
        String check = SelectedArea.getText();

        if(check.equals("")) {
            JOptionPane.showMessageDialog(null,"Add something first!");
        } else {
            int reply = JOptionPane.showConfirmDialog(null, message,title,JOptionPane.YES_NO_OPTION);
            if(reply == JOptionPane.YES_OPTION) {
                SelectedArea.setText("");
                String query = "DELETE FROM ORDER_LIST WHERE FOOD_TYPE='Main Dish'";
                try {
                    stat = con.createStatement();
                    stat.executeUpdate(query);

                } catch (SQLException ex) {
                    Logger.getLogger(MENU_SELECTION_APP.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        // TODO add your handling code here:
}//GEN-LAST:event_ResetActionPerformed

    private void populateData() throws SQLException
    {
        String query = "SELECT FOOD_NAME FROM FOOD WHERE FOOD_TYPE = 'Main Dish'";
        M_Dish = new DefaultListModel();
        rset = getResultSet(query);
        while(rset.next())
        {
            M_Dish.addElement(rset.getString(1));
        }
        MainList.setModel(M_Dish);
        MainList.setSelectedIndex(0);

    }

    private ResultSet getResultSet(String s) throws SQLException {
        connect();
        ResultSet rs = null;
        try {
            stat = con.createStatement();
            rs = stat.executeQuery(s);
        } catch (SQLException ex) {
            Logger.getLogger(MENU_SELECTION_APP.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rs;
    }

    private void connect() throws SQLException
    {
         try {
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            System.out.println("Driver Loaded");
            con = DriverManager.getConnection(url,pass_id,pass_pw);
            System.out.println("Database Connected");

         }
         catch (Exception ex)
         {
            Logger.getLogger(MENU_SELECTION_MAIN.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void takeout() throws SQLException
    {
        String query = "SELECT FOOD_NAME FROM ORDER_LIST WHERE FOOD_TYPE='Main Dish'";
        rset = getResultSet(query);
        while(rset.next())
        {
            SelectedArea.append(rset.getString("FOOD_NAME") + newline);
        }

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MENU_SELECTION_MNDISH().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(MENU_SELECTION_MNDISH.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JButton Done;
    private javax.swing.JPanel InfoPane;
    private javax.swing.JList MainList;
    private javax.swing.JComboBox QuantityNum;
    private javax.swing.JButton Reset;
    private javax.swing.JTextArea SelectedArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLayeredPane jLayeredPane4;
    private javax.swing.JLayeredPane jLayeredPane5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JSeparator jSeparator8;
    // End of variables declaration//GEN-END:variables

}
